package com.hzw.saas.service.product.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.product.IProductService;
import com.hzw.saas.api.product.ITrialApplicationService;
import com.hzw.saas.api.product.dto.ProductDto;
import com.hzw.saas.api.product.query.TrialApplicationAddQuery;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.util.Json;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.common.util.enums.AssertEnum;
import com.hzw.saas.service.product.mapper.TrialApplicationMapper;
import com.hzw.saas.service.product.model.TrialApplication;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author zzl
 * @since 2021/12/30
 */
@Service(TrialApplicationServiceImpl.BEAN_NAME)
@RequiredArgsConstructor
@Slf4j
public class TrialApplicationServiceImpl extends ServiceImpl<TrialApplicationMapper,TrialApplication> implements ITrialApplicationService, IService<TrialApplication> {

    public static final String BEAN_NAME = "TrialApplicationServiceImpl";

    private final IProductService productService;

    @Override
    public void saveTrialApplication(TrialApplicationAddQuery query) {
        log.debug("保存试用申请：{}", Json.toJsonString(query));
        TrialApplication entity = MapperUtil.nf().map(query, TrialApplication.class);

        ProductDto product = productService.getProductById(query.getProductId());
        AssertUtil.assertExistence(AssertEnum.FOUND,product,"产品/软件");

        entity.setPid(SnowFlakeUtil.nextIdStr());
        entity.setProductName(product.getProdName());
        super.save(entity);
    }
}
